
package com.xiyuan.smartutils.client.ws.core.impl;

import com.xiyuan.smartutils.client.ws.core.handler.StreamHandler;
import com.xiyuan.smartutils.client.exception.WebSocketException;

import java.nio.ByteBuffer;

/**
 * 
 * The class WebSocketPipeline
 *
 * @author xiyuan 2022-07-10 @version 1.0.0 新建或整理
 *
 */
public class WebSocketPipeline
{

    private final StreamHandler handler;

    public WebSocketPipeline(StreamHandler handler)
    {
        super();
        this.handler = handler;
    }

    /**
     * Send packet data;
     * @param ws
     * @param appBuffer
     * @throws WebSocketException
     */
    public void upstream(WebSocketInstance ws, ByteBuffer appBuffer) throws WebSocketException
    {
        this.handler.write(ws, appBuffer);
    }

    /**
     * Send packet data to Handler
     * @param ws
     * @param netBuffer
     * @throws WebSocketException
     */
    public void downstream(WebSocketInstance ws, ByteBuffer netBuffer) throws WebSocketException
    {
        this.handler.read(ws, netBuffer);
    }

    /**
     * Send ws handshake. (http Protocols to switching Protocols)
     * @param ws
     * @param handshakeRequest
     * @throws WebSocketException
     */
    public void upstreamHandshake(WebSocketInstance ws, ByteBuffer handshakeRequest) throws WebSocketException
    {
        this.handler.writeHandshake(ws, handshakeRequest);
    }

    /**
     * Send HttpResponse to Handler
     * @param ws
     * @param netBuffer
     */
    public void downstreamHandshake(WebSocketInstance ws, ByteBuffer netBuffer) throws WebSocketException
    {
        this.handler.readHandshake(ws, netBuffer);
    }

}
